﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Luna小店.service;
using DevExpress.Data;
using Luna小店.UC.门店下单.DTO;
using Luna小店.UC.款式异常;
using Luna小店.UC.门店下单.form.订单修改;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using Luna小店.model;
using Luna小店.DTO;
using DevExpress.XtraGrid.Demos.util;
using mendian;

namespace Luna小店.UC.门店下单.form
{

    public partial class Frm订单一览 : DevExpress.XtraEditors.XtraForm
    {
        public List<OrderStatusIndexDTOShow> orderStatusIndexDTOShows { get; set; } = new List<OrderStatusIndexDTOShow>();
        private List<OrderStatusIndexDTO> orderStatusIndexDTOs = new List<OrderStatusIndexDTO>();
        public Enum订单类别 enum订单类别 = Enum订单类别.销售订单;
        public enum Enum订单类别 { 销售订单, 样品订单, 坚山订单, 坚山美国订单, 套号订单, Luna店铺订单 };//订单类型
        private List<Order_Status> Order_Status = new List<Order_Status>();
        public Frm订单一览()
        {
            InitializeComponent();

        }
        public Frm订单一览(Enum订单类别 @enum)
        {
            this.enum订单类别 = @enum;
            InitializeComponent();
        }
        /// <summary>
        /// 初始化状态数据
        /// </summary>
        private void initStatusData()
        {
            this.status_code = null;
            this.status_name = "全部";
            this.searchLookUpEdit1.EditValue = this.status_name;

            this.Order_Status = OrderService.getOrderStatus();
            this.searchLookUpEdit1.Properties.DataSource = this.Order_Status;
        }

        private void Frm已付款订单一览_Load(object sender, EventArgs e)
        {
            initStatusData();   //  初始化状态数据
            List<String> status = new List<string>();
            if (String.IsNullOrEmpty(this.status_code))
            {
                foreach (Order_Status orderStatus in this.Order_Status)
                {
                    status.Add(orderStatus.code);
                }
            }
            else
            {
                status.Add(this.status_code);
            }
            if (this.enum订单类别 == Enum订单类别.销售订单)
            {
                this.gridControl订单一览.DataSource = OrderService.get订单(status);
                //this.gridView1.BestFitColumns();
            }
            else if (this.enum订单类别 == Enum订单类别.样品订单)
            {
                this.gridControl订单一览.DataSource = OrderService.Get样品订单(status);
            }
            else if (this.enum订单类别 == Enum订单类别.Luna店铺订单)
            {
                this.orderStatusIndexDTOs = FunctionHelper.JsonDeserialization<List<OrderStatusIndexDTO>>(RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderStatusIndexPageable?shopId=51&pageStart=0&pageSize=100"));
                foreach (OrderStatusIndexDTO dto in this.orderStatusIndexDTOs)
                {
                    this.orderStatusIndexDTOShows.Add(new OrderStatusIndexDTOShow(dto));
                }
                //this.gridControl订单一览.DataSource = OrderService.GetM2店铺订单(status);
                this.gridControl订单一览.DataSource = this.orderStatusIndexDTOShows;
            }
            this.gridView1.Columns["ORDER_DATE"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.ExpandAllGroups();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            List<尺寸呈现dto> lst = new List<尺寸呈现dto>();
            try
            {
                lst = SizeService.getDto尺寸ByOrderId(
                    this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["STYLE_FIT_CD"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["STYLE_CATEGORY_CD"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["STYLE_SIZE_CD"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["STYLE_SIZE_GROUP_CD"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["CUSTOMER_ID"].ToString()
                    );
            }
            catch
            {
            }
            DTOStyleMeasureItems dTO = new DTOStyleMeasureItems();
            dTO = FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(RestCall.httpGetMethod(URL.接口url + "/style/getMeasureValue?styleID=" + FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "STYLE_ID")));
            //  CUSTOMER_ID
            if (this.enum订单类别 == Enum订单类别.销售订单)
            {
                new Frm订单预览(
                    this.gridView1.GetDataRow(e.RowHandle)["STYLE_ID"].ToString()
                    , lst
                    , this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString()
                    , this.gridView1.GetDataRow(e.RowHandle)["REMARKS"].ToString()
                    , Enum订单类别.销售订单
                    , dTO
                    ).HideButton套号吊牌().HideFor任务().ShowDialog();
            }
            if (this.enum订单类别 == Enum订单类别.Luna店铺订单)
            {
                new Frm订单预览(
                    FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "STYLE_ID").ToString()
                    , lst
                    , FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "ORDER_ID").ToString()
                    , FunctionHelper.GetgridViewValue(this.gridView1, e.RowHandle, "REMARKS").ToString()
                    , Enum订单类别.销售订单
                    , dTO
                    ).HideButton套号吊牌().HideFor任务().ShowDialog();
            }
            else if (this.enum订单类别 == Enum订单类别.样品订单)
            {
                new Frm订单预览(this.gridView1.GetDataRow(e.RowHandle)["STYLE_ID"].ToString(), lst, this.gridView1.GetDataRow(e.RowHandle)["ORDER_ID"].ToString(), this.gridView1.GetDataRow(e.RowHandle)["REMARKS"].ToString(), Enum订单类别.样品订单).HideButton().HideFor任务().ShowDialog();
            }

        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void refreshData()
        {
            List<String> status = new List<string>();
            status.Add(this.status_code);
            if (this.enum订单类别 == Enum订单类别.销售订单)
            {
                this.gridControl订单一览.DataSource = OrderService.get订单(status);
            }
            else if (this.enum订单类别 == Enum订单类别.样品订单)
            {
                this.gridControl订单一览.DataSource = OrderService.Get样品订单(status);
            }
            this.gridView1.Columns["ORDER_DATE"].SortOrder = ColumnSortOrder.Descending;
            this.gridView1.RefreshData();
            this.gridView1.ExpandAllGroups();
        }


        #region 选择门店
        private String status_code;
        private String status_name;
        private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                ////得到取消按钮，重写点击事件
                //Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                //LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                //clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                //LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                //myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            var a = ((SearchLookUpEdit)sender).Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                try
                {
                    this.status_code //  no
                        = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "code").ToString();//id 是 Value Member
                }
                catch { }

                this.status_name //  no
                    = ((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "name").ToString();//id 是 Value Member
            }

            this.refreshData();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit1.ToolTip = null;
            this.searchLookUpEdit1.EditValue = null;
        }
        private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.status_name;
        }
        #endregion

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.gridControl订单一览.DataSource = OrderService.get订单ByORDER_NO(this.textEdit订单号.Text);
        }

        private void 已付款ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SQLmtm.DoUpdate("o_order_p", new string[] { "ORDER_STATUS_CD" }, new string[] { "ORDER_STATUS-OS_13" }, new string[] { "ORDER_ID" }, new string[] { gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_ID").ToString() });
                MessageBox.Show("已改为已付款");
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void 待付款ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SQLmtm.DoUpdate("o_order_p", new string[] { "ORDER_STATUS_CD" }, new string[] { "ORDER_STATUS-OS_12" }, new string[] { "ORDER_ID" }, new string[] { gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_ID").ToString() });
                MessageBox.Show("已改为待付款");
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_ID").ToString();
                SQLmtm.DoUpdate("o_order_p", new string[] { "ORDER_STATUS_CD" }, new string[] { "ORDER_STATUS-OS_80" }, new string[] { "ORDER_ID" }, new string[] { orderId });
                RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                MessageBox.Show("已改为取消订单");
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.gridControl订单一览.DataSource = OrderService.get坚山订单ByCUSTOMER_NAME(this.textEdit坚山客户名称.Text);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>(this.textBox单号多行.Text.Replace("\r", "").Split('\n'));
            DataTable DataTable = OrderService.get坚山订单sByOrderN0s(list);
            foreach (DataRow dataRow in DataTable.Rows)
            {
                new 订单导入model(
                    dataRow["STYLE_ID"].ToString()
                    , dataRow["ORDER_ID"].ToString()
                    , Enum订单类别.销售订单
                    );
            }
        }

        private void 删除订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(
                "是否确定将" + this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_NO").ToString() + "订单删除",
                "是否删除该订单",
                MessageBoxButtons.YesNo) == DialogResult.Yes
                )
            {
                if (OrderService.DeleteOrder(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "ORDER_ID").ToString()) == true)
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
    }

    /// <summary>
    /// 订单状态
    /// </summary>
    public class Order_Status
    {
        public String code { get; set; }
        public String name { get; set; }

        public Order_Status(String name)
        {
            this.name = name;
        }
        public Order_Status(DataRow dataRow)
        {
            this.code = dataRow["ITEM_VALUE"].ToString();
            this.name = dataRow["ITEM_NAME_CN"].ToString();
        }
    }
}